package com.xbb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbb.entity.*;
import com.xbb.service.ITrackService;
import com.xbb.util.Result;
import com.xbb.util.Result1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.awt.datatransfer.Transferable;
import java.util.List;

/**
 * <p>
 *  追踪信息控制层
 * </p>
 *
 * @author Mr xie
 * @since 2020-06-03
 */
@RestController
@RequestMapping("/track")
public class TrackController {

    @Autowired
    ITrackService iTrackService;

    @PostMapping("/getTrackList")
    public Object getTrackList(TrackParam trackParam){
        QueryWrapper<Track> queryWrapper = new QueryWrapper<Track>();
        if (!StringUtils.isEmpty(trackParam.getStuId())) {
            queryWrapper.like("stu_id", trackParam.getStuId());
        }
        if (!StringUtils.isEmpty(trackParam.getBeginTime())) {
            queryWrapper.gt("track_time", trackParam.getBeginTime());
        }
        if (!StringUtils.isEmpty(trackParam.getEndTime())) {
            queryWrapper.lt("track_time", trackParam.getEndTime());
        }
        queryWrapper.orderByDesc("id");
        Page<Track> page = new  Page<Track>(trackParam.getPage(),trackParam.getLimit());
        IPage<Track> iPage = iTrackService.page(page, queryWrapper);
        List<Track> data = iPage.getRecords();
        long total = iPage.getTotal();
        return new Result(0,"用户列表",total,data);
    }

    /**
     * 添加追踪记录
     * @param stuId
     * @param track
     * @return
     */
    @PostMapping("/addTrack")
    public Object addTrack(@RequestParam Integer stuId, Track track){
        track.setStuId(stuId);//（暂时没用到，学生id在前端已经获取）
        boolean res = iTrackService.save(track);
        if(res) {
            return new Result(0,"添加成功");
        }else {
            return new Result(1,"添加失败");
        }
    }

    /**
     * 根据学生id获取追踪记录
     * @param trackParam
     * @param stuId
     * @return
     */
    @RequestMapping("/getStuTrack")
    public Object getStuTrack(TrackParam trackParam,Integer stuId) {
        trackParam.setStuId(stuId);
        System.out.println("11111"+stuId);
        List<TrackParam> list = iTrackService.getStuTrack(trackParam);
        trackParam.setPage(null);
        trackParam.setLimit(null);
        List<TrackParam> list2 = iTrackService.getStuTrack(trackParam);
        return new Result1(0, "查询成功", list2.size(), list);
    }


}
